<template>
  <gt_guild>
    <template v-slot:nocard v-if="gd.gload == false">
      <template v-if="gd.guild?.boss_data?.data?.length >= 1">
        <div class="nbox1">
          <m_select
            :select="val.select"
            :click="ct.select_click"
            :qs="val.qs"
            :op="val.op"
          >
            <template #sea>
              <el-select
                :style="`min-width: ${cfg.mobile ? 8 : 8}rem`"
                :size="cfg.mobile ? 'small' : ''"
                style="min-width: 12rem"
                :reserve-keyword="false"
                filterable
                placeholder="主控筛选"
                v-model="val.qs.zk"
                @change="ct.select_click"
              >
                <el-option :label="'移除筛选'" :value="''">
                  <div
                    style="
                      display: flex;
                      align-items: center;
                      margin-top: 3px;
                      margin-bottom: 3px;
                    "
                  >
                    <span>移除主控角色筛选</span>
                  </div>
                </el-option>
                <el-option
                  v-for="(item, key) in cfg.role"
                  :label="item.name"
                  :value="item.icon"
                >
                  <div class="el_role_box">
                    <img class="workairole" :src="cfg.function.ava(item.icon)" />
                    <span>　{{ item.name }}</span>
                  </div>
                </el-option>
              </el-select>
            </template>
          </m_select>

          <div class="tbdiv">
            <table class="ml_table1" style="min-width: 770px">
              <thead>
                <tr>
                  <th class="ml_table1_th1" width="240px">首领 / 队伍信息</th>
                  <th width="70px">整刀次数</th>
                  <th width="100px">平均伤害</th>
                  <th width="100px">最高伤害</th>
                  <th width="120px">累计伤害</th>
                  <th v-if="cfg?.guild?.season <= 2000" width="90px">范围等级</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                <tr
                  v-for="(item, key) in val.data"
                  style="background: rgb(65 71 85 / 25%)"
                >
                  <td>
                    <div class="stsicon">
                      <div class="bava_div">
                        <img
                          :class="`bava bava_${item.boss}`"
                          :src="cfg.function.boss_ava(item.boss, 40)"
                        />
                      </div>
                      <template v-for="(item2, key2) in item?.team?.split(',')">
                        <img class="xdava" :src="cfg.function.ava(item2)" />
                      </template>
                    </div>
                  </td>
                  <td style="text-align: center">
                    <div class="stsadmg">{{ item.count }}</div>
                  </td>
                  <td style="text-align: center">
                    <n-popover trigger="hover" :keep-alive-on-hover="false">
                      <template #trigger>
                        <div class="stsadmg">{{ item.avgdmg }}</div>
                      </template>
                      <span>{{ item.avgdmg / 10000 }} 万</span>
                    </n-popover>
                  </td>
                  <td style="text-align: center">
                    <n-popover trigger="hover" :keep-alive-on-hover="false">
                      <template #trigger>
                        <div class="stsadmg">{{ item.maxdmg }}</div>
                      </template>
                      <span>{{ item.maxdmg / 10000 }} 万</span>
                    </n-popover>
                  </td>
                  <td style="text-align: center">
                    <n-popover trigger="hover" :keep-alive-on-hover="false">
                      <template #trigger>
                        <div class="stsadmg">{{ item.sumdmg }}</div>
                      </template>
                      <span
                        >{{ item.sumdmg / 100000000 }} 亿<br />{{
                          item.sumdmg / 10000
                        }}
                        万</span
                      >
                    </n-popover>
                  </td>
                  <td style="text-align: center" v-if="cfg?.guild?.season <= 2000">
                    <n-tag type="warning"
                      >Lv. {{ item.minlevel ?? "?" }} - {{ item.maxlevel ?? "?" }}</n-tag
                    >
                  </td>
                  <td style="text-align: center">
                    <n-space> </n-space>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div> </template></template
  ></gt_guild>
</template>

<script>
//2024/07/17 猫燐
import { cfg } from "@/cfg/cfg.js";
import { getrole } from "@/cfg/function.js";
import gt_guild from "@/comp/gt_guild.vue";
import m_select from "@/comp/m_select.vue";

export default {
  components: {
    gt_guild,
    m_select,
  },
  setup() {
    let gd = cfg.guild;
    let val = reactive({
      coldw: cfg.mobile == true ? 500 : 500,
      cols: 1,
      reportdata: [],
      //选择菜单选中
      qs: {},
      //选择菜单列表
      op: {
        boss: [],
        level: [],
        player: [],
      },
    });

    val.select = [
      {
        tips: "选择首领",
        val: "boss",
        width: 8,
        mwidth: 8,
      },
      {
        tips: "等级筛选",
        val: "level",
        width: 8,
        bans2: true,
        mwidth: 8,
      },
      {
        tips: "玩家筛选",
        val: "player",
        width: 8,
        mwidth: 8,
      },
    ];

    let ct = reactive({});

    ct.select_click = () => {
      ct.data();
    };

    ct.data = () => {
      val.data = {};
      gd.alldao?.forEach((i, k) => {
        if (val.qs.player != "all" && i.user_id != val.qs.player) {
          return;
        }
        if (val.qs.zk != null && i.zk != val.qs.zk) {
          return;
        }
        if (val.qs.boss != "all" && i.boss_name != val.qs.boss) {
          return;
        }
        if (val.qs.level == "94-100") {
          if (i.level < 94) {
            return;
          }
        } else {
          if (val.qs.level != null && val.qs.level != i.level) {
            return;
          }
        }
        const group = i.icon + i.boss_name;
        //不存在则初始化
        if (val.data[group] == null) {
          val.data[group] = {
            boss: i.boss_name,
            team: i.icon,
            count: 0,
            avgdmg: 0,
            maxdmg: 0,
            sumdmg: 0,
            minlevel: 0,
            maxlevel: 0,
          };
        }
        //计算刀数、总伤
        val.data[group].count += i.cr_kill;
        val.data[group].sumdmg += i.damage;
        //计算最大伤害值
        if (i.damage > val.data[group].maxdmg) {
          val.data[group].maxdmg = i.damage;
        }
        //计算最小等级
        if (val.data[group].minlevel == 0 || val.data[group].minlevel > i.level) {
          val.data[group].minlevel = i.level;
        }
        //计算最大等级
        if (val.data[group].maxlevel == 0 || val.data[group].maxlevel < i.level) {
          val.data[group].maxlevel = i.level;
        }
        //计算均伤
        val.data[group].avgdmg = parseInt(val.data[group].sumdmg / val.data[group].count);
      });
      //obj转为数组
      val.data = Object.values(val.data);
    };

    //加载公会初始化
    gd.runfunc = async () => {
      gd.vuename = "公会刀型";
      await gd.getdao();
      val.cols = parseInt(cfg.sswd / val.coldw);

      //如果角色数据没有icon重新加载
      if (cfg.role?.[0]?.icon == null) {
        await getrole();
      }

      gd.guild.user_data?.forEach((item, key) => {
        val.op.player.push({
          value: item.user_id,
          label: item.user_name,
        });
      });

      gd.guild.boss_data?.data?.forEach((item, key) => {
        val.op.boss.push({
          value: item.boss_name,
          label: `${item.boss_name}`,
        });
      });

      gd.guild?.level?.forEach((item, key) => {
        val.op.level.push({
          value: item,
          label: `Level.${item}`,
        });
      });

      if (gd.guild?.level?.[0] >= 94) {
        val.qs.level = "94-100";
      } else {
        val.qs.level = gd.guild?.level?.[0];
      }

      ct.data();
    };

    //如果触发更新公会
    gd.updfunc = async () => {};

    watch(
      () => cfg.sswd,
      (newWidth, oldWidth) => {
        val.cols = parseInt(cfg.sswd / val.coldw);
      }
    );

    onMounted(async () => {
      val.op.boss = [
        {
          value: "all",
          label: `所有首领`,
        },
      ];

      val.op.player = [
        {
          value: "all",
          label: `所有成员`,
        },
      ];

      val.op.level = [
        {
          value: null,
          label: `移除等级筛选`,
        },
        {
          value: "94-100",
          label: "Level.94-100",
        },
      ];

      val.qs.boss = "all";
      val.qs.player = "all";
    });

    return {
      cfg,
      ct,
      gd,
      val,
    };
  },
};
</script>
